//
//  VRWebView.swift
//  vr_web
//
//  Created by 李旭峰 on 2020/7/13.
//

import Flutter
import SnapKit
import RxSwift
import RxCocoa

class VRWebView: NSObject {
    private let dispose = DisposeBag()
    static let viewName:String = "plugins.com.asman/vr"
    private let frame:CGRect
    private let viewID:Int64
    private let args:Any?
    var messenger:FlutterBinaryMessenger
    private var myView:UIView = UIView()
    private var channel:FlutterMethodChannel?
    private var isDidInit:Bool = false
    
    init(_ frame:CGRect,viewID:Int64,args:Any?,binaryMessenger:FlutterBinaryMessenger) {
        self.frame = frame
        self.viewID = viewID
        self.args = args
        self.messenger = binaryMessenger
        super.init()
        self.myView.isUserInteractionEnabled = true
        self.myView.frame = self.frame
//        self.myView.delegate = self
//        if let params = args as? [String:Any],let urlPath = params["urlPath"] as? String,let url = URL(string: urlPath) {
//            self.myView.resetUrlPath(url)
//        }
//        if let params = args as? [String:Any] {
//            self.myView.shareBaseUrl = String.SwitchToString(params["shareBaseUrl"]) ?? ""
//        }
//        if let params = args as? [String:Any] {
//            self.myView.tenantId = String.SwitchToString(params["tenantId"]) ?? ""
//        }
//        if let params = args as? [String:Any] {
//            self.myView.userId = String.SwitchToString(params["userId"]) ?? ""
//        }
    }
    
    private func initMathodChannel(){
        let channel = FlutterMethodChannel.init(name: "plugins.com.asman/vr_\(self.viewID)", binaryMessenger: self.messenger)
        self.channel = channel
        self.channel?.setMethodCallHandler({[weak self] (call, result) in
            guard let `self` = self else  { return }
            switch call.method {
            case "trtcExitRoom":
                AsmanLiteManager.default.exitRoom { (success, error, res) in
                    result("")
                }
            default:result("");
            }
        })
    }
}

extension VRWebView : FlutterPlatformView {
    func view() -> UIView {
        if !self.isDidInit {
            self.isDidInit = true
            self.initMathodChannel()
        }
        return self.myView
    }
}
//extension VRWebView : SchemeProgramViewDelegate {
//    func routeToDecorateDetail(with schemeId: String, workId: String?, hotspots: [String : Any]?) {
//        var arguments:[String:Any] = [:]
//        arguments["schemeId"] = schemeId
//        if let tempWorkId = workId {
//            arguments["workId"] = tempWorkId
//        }
//        if let tempHotspots = hotspots {
//            arguments["hotspots"] = tempHotspots
//        }
//        let argumentsString = (arguments as NSDictionary).jsonString() ?? ""
//        self.channel?.invokeMethod("routeToDecorateDetail", arguments: argumentsString);
//    }
//    func onGoBack(_ params: String) {
//        self.channel?.invokeMethod("onGoBack", arguments: params);
//    }
//
//    func getTrtcAuthInfo() {
//        self.channel?.invokeMethod("getTrtcAuthInfo", arguments: [:]);
//    }
//    func notifyDesignerJoined(){
//        self.channel?.invokeMethod("notifyDesignerJoined", arguments: [:]);
//    }
//
//    func switchToDesignerShopView(_ designerId:String){
//        self.channel?.invokeMethod("switchToDesignerShopView", arguments: designerId.decimal.int);
//    }
//}
